/**
 * @author zhouheng
 * @date 2022年02月07日 22:50
 */

package com.atguigu.common.valid;

import com.google.common.collect.Sets;
import java.util.Arrays;
import java.util.Set;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class ListValueConstraintValidator implements ConstraintValidator<ListValue, Integer> {
    Set<Integer> set = Sets.newHashSet();

    @Override
    public void initialize(ListValue constraintAnnotation) {
        int[] vals = constraintAnnotation.vals();
        Arrays.stream(vals).forEach(t -> {
            set.add(t);
        });

    }

    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext context) {
        return set.contains(value);
    }
}
